﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using CMISLibrary.Libraries;
using BaocaoNPC.Usercontrols.reports;
using BaocaoNPC.common;
using CMISLibrary;
using BCNPCEntities;
using System.Threading;

namespace CMIS2.BaocaoNPC.Usercontrols
{
    /// <summary>
    /// Interaction logic for NPC_BCKD00.xaml
    /// </summary>
    public partial class NPC_BCKD00 : UserControl
    {
        #region KhaiBaoBien
        /*
        private string strMa_dviqly = UserInfomation.MaDViqly != "" && UserInfomation.MaDViqly != null ? UserInfomation.MaDViqly : "PA25";
        private string strTenDNHap = UserInfomation.TenDNHap != "" && UserInfomation.TenDNHap != null ? UserInfomation.TenDNHap : "PD0200";
        private string strCurrentLibID = MenuInfomation.LibID != "" && MenuInfomation.LibID != null ? MenuInfomation.LibID : "65";
        private string strTen_dviqly = UserInfomation.TenDViQLy != "" && UserInfomation.TenDViQLy != null ? UserInfomation.TenDViQLy : "Dien luc Vinh Phuc";
        private string strTen_dviqly_CT = UserInfomation.TenDViQLy_CT != "" && UserInfomation.TenDViQLy_CT != null ? UserInfomation.TenDViQLy_CT : "Cong ty Dien luc Vinh Phuc";
         * */

        public String strMa_dviqly = NPCUS_DanhMuc.Pglobal.strMa_dviqly != "" && NPCUS_DanhMuc.Pglobal.strMa_dviqly != null ? NPCUS_DanhMuc.Pglobal.strMa_dviqly : "PA";
        public String strTenDNHap = NPCUS_DanhMuc.Pglobal.strTen_dangnhap != "" && NPCUS_DanhMuc.Pglobal.strTen_dangnhap != null ? NPCUS_DanhMuc.Pglobal.strTen_dangnhap : "PA";
        public String strTen_dviqly = NPCUS_DanhMuc.Pglobal.strTen_dviqly != "" && NPCUS_DanhMuc.Pglobal.strTen_dviqly != null ? NPCUS_DanhMuc.Pglobal.strTen_dviqly : "cty..";
        public String strTen_dviqly_CT = NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT != "" && NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT != null ? NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT : "cap tren";

        string strError = "";
        string strThemSuaXoa = "";
        string strTim = "";//"CoDuLieu"; "KhongCoDuLieu"
        string strThongBao = "",MASOGOC ="",MASONGON="";
        Boolean kt;
        Int16 p_thang,ifthang;
        Int16 p_nam,ifnam,row;
        int dem;
        int intDongTrenGrid;
        DataSet ds;
        DataSet dshuynhap;
        DataSet dsbieu05_Entity;
        DataSet dsbieu00_Entity;
        ListItem lstItem;
        DataTable dtHienThi = new DataTable();
        DataTable dtbieu00 = new DataTable();
        #endregion

        //============================================================================================================================
        #region KhoiTaoThongTinKhiLoadForm

        private void setEntity(string str)
        {
            try
            {
                switch (str)
                {
                    case "1": //Loadcombo
                        dsbieu05_Entity.Tables["D_DVI_QLY"].Rows.Clear();
                        DataRow dr = dsbieu05_Entity.Tables["D_DVI_QLY"].NewRow();
                        dr["TEN_DVIQLY"] = cboten_donvi.Text.Trim();
                        //break;
                        dsbieu05_Entity.Tables["D_DVI_QLY"].Rows.Add(dr);
                        dsbieu05_Entity.AcceptChanges();
                        break;
                    case "2": //Loadgrid
                        dsbieu00_Entity.Tables["PC1_BCKD00"].Rows.Clear();
                        DataRow dr1 = dsbieu00_Entity.Tables["PC1_BCKD00"].NewRow();
                        //dr1["DOI_TUONG"] = " ";
                        //dr1["TS_KH"] = "";
                        //break;
                        dsbieu00_Entity.Tables["PC1_BCKD00"].Rows.Add(dr1);
                        dsbieu00_Entity.AcceptChanges();
                        break;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void InitCombo_cbo()
        {
            setEntity("1");
            //PC1Business.GlobalServices.Service_NPCUS_D_DVI_QLY GlobalServices.Service_NPCUS = new PC1Business.GlobalServices.Service_NPCUS_D_DVI_QLY();
            //bus_TienichNPC.cls_TienichNPC_bus cls = new bus_TienichNPC.cls_TienichNPC_bus();
            //DataSet dscobo = cls.select_D_DVI_QLY_ALL(strMa_dviqly);
            DataSet dscobo = GlobalServices.Service_NPCUS.select_D_DVI_QLY_ALL(strMa_dviqly);
            dtHienThi = dscobo.Tables[0].Copy();
            string strdonvi = "";
            if (dtHienThi.Rows.Count > 0)
            {
                foreach (DataRow row in dtHienThi.Rows)
                {
                    lstItem = new ListItem();
                    strdonvi = Convert.ToString(row["TEN_DVIQLY"]);
                    //lstItem.v = Convert.ToString(row["TEN_DVIQLY"]);
                    cboten_donvi.Items.Add(strdonvi);
                    cboten_donvi.SelectedIndex = 0;
                }
            }
        }
        private void Initgrid(string ma_donvi)
        {
            setEntity("2");
            if (ma_donvi.ToString().Length == 4)
            {
                //PC1Business.GlobalServices.Service_NPCUS_pc1_bieu18 GlobalServices.Service_NPCUS1 = new PC1Business.GlobalServices.Service_NPCUS_pc1_bieu18();
                //DataSet dscobocty = GlobalServices.Service_NPCUS.Chon_cty_dienluc(p_nam, p_thang, ma_donvi, 18);
            }
            //bus_TienichNPC.cls_TienichNPC_bus cls = new bus_TienichNPC.cls_TienichNPC_bus();
            //DataSet dscobo = cls.select_PC1_BCKD00(ma_donvi, p_thang, p_nam); 
           dsbieu00_Entity = GlobalServices.Service_NPCUS.select_PC1_BCKD00(ma_donvi, p_thang, p_nam);
           dshuynhap = dsbieu00_Entity.Copy();
            //dtbieu00 = dscobo.Tables[0].Copy();
            //dtbieu00.Columns["DVT"].
            //DataSet dscobo = GlobalServices.Service_NPC.select_donviqly(dsD_BCKD_Entity);
           if (dsbieu00_Entity.Tables["PC1_BCKD00"].Rows.Count > 0)
            {
                dgrNPC_BCKD00.ItemsSource = dsbieu00_Entity.Tables["PC1_BCKD00"].DefaultView;
                // CheckIsEnableButtonSuaXoa();
                dgrNPC_BCKD00.SelectedIndex = 0;
                txtPT_DG.Text = dsbieu00_Entity.Tables["PC1_BCKD00"].Rows[1]["PHANTICH_DANHGIA"].ToString();
                txtYKIEN_KHAC.Text = dsbieu00_Entity.Tables["PC1_BCKD00"].Rows[1]["YKIEN_KHAC"].ToString();

            }
        }
        #endregion

        //============================================================================================================================
        
        //============================================================================================================================
        public NPC_BCKD00()
        {
            try
            {

                InitializeComponent();
                dsbieu05_Entity = new BCNPCEntities.CMIS2.D_DVI_QLY();
                dtHienThi = dsbieu05_Entity.Tables["D_DVI_QLY"].Clone();
                dsbieu00_Entity = new BCNPCEntities.BCKD15.PC1_D_BCKD_Entity();
                dtbieu00 = dsbieu00_Entity.Tables["PC1_BCKD00"].Clone();
                GetThangNamBC();
                InitCombo_cbo();
                khoitao_bieu00();
                dgrNPC_BCKD00.IsReadOnly = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void khoitao_bieu00()
        {
            p_thang = Convert.ToInt16(dtpThangNam.SelectedDate.Value.Month);
            p_nam = Convert.ToInt16(dtpThangNam.SelectedDate.Value.Year);
            //bus_TienichNPC.cls_TienichNPC_bus cls = new bus_TienichNPC.cls_TienichNPC_bus();
            //ds = cls.KHOITAO_BIEU00(p_nam, p_thang, strMa_dviqly);
            ds = GlobalServices.Service_NPCUS.KHOITAO_BIEU00(p_nam, p_thang, strMa_dviqly);
            // MessageBox.Show(drv.Row["TS_KH"].ToString());
        }
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            {
                cmdhuy.IsEnabled = false;
                cmdnhap.IsEnabled = false;
                cmdghi.IsEnabled = false;
                //cmddel.Visibility = false;
                cmddel.IsEnabled = false;
                GetThangNamBC();
                if (strMa_dviqly.ToString().Length <= 4)
                {
                    cmdtonghop.IsEnabled=true;
                }
                else
                {
                    cmdtonghop.IsEnabled = false;
                }
            }
        }

        private void GetThangNamBC()
        {
            try
            {
                GET_THANGNAM();

                if (p_thang.ToString().Length == 1)
                {
                    dtpThangNam.Text = "0" + p_thang.ToString() + "/" + p_nam.ToString();
                }
                else
                {
                    dtpThangNam.Text = p_thang.ToString() + "/" + p_nam.ToString();
                }

                //========      Xac dinh ngay cuoi thang de hien thi vao datetime picker    ========
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message.ToString());
            }
        }
        private void GET_THANGNAM()
        {
            try
            {
                DataSet dsthanglviec = new DataSet();
                dsthanglviec = GlobalServices.Service_NPCUS.get_thang_lviec();
                if (dsthanglviec != null && dsthanglviec.Tables.Count > 0)
                {
                    foreach (DataRow dr in dsthanglviec.Tables[0].Rows)
                        if (dr["PHAN_HE"].ToString() == "BC" && dr["MA_DVIQLY"].ToString() == strMa_dviqly)
                        {
                            p_thang = Convert.ToInt16(dr["THANG"]);
                            p_nam = Convert.ToInt16(dr["NAM"]);
                            ifthang = Convert.ToInt16(dr["THANG"]);
                            ifnam = Convert.ToInt16(dr["NAM"]);
                        }
                }

            }
            catch
            {
                ifthang = Convert.ToInt16(DateTime.Now.Month);
                ifnam = Convert.ToInt16(DateTime.Now.Year);
                p_thang = Convert.ToInt16(DateTime.Now.Month);
                p_nam = Convert.ToInt16(DateTime.Now.Year);
            }
        }
        private void cmdin_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                for (int i = 0; i < dtHienThi.Rows.Count; i++)
                {
                    if (cboten_donvi.Text.ToString() == dtHienThi.Rows[i]["TEN_DVIQLY"].ToString())
                    {
                        strMa_dviqly = dtHienThi.Rows[i]["MA_DVIQLY"].ToString();
                    }
                }
                //Hien thi con tro bao ban, khi bat dau nhan nut In
                p_thang = Convert.ToInt16(dtpThangNam.SelectedDate.Value.Month);
                p_nam = Convert.ToInt16(dtpThangNam.SelectedDate.Value.Year);
                this.Cursor = Cursors.Wait;
                Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, new ThreadStart(delegate { }));

                DataSet ds1 = new DataSet();
                setEntity("2");
                //dscobo.WriteXmlSchema("D://PC1_BCKD00.xsd");
                DataSet dscobo = GlobalServices.Service_NPCUS.select_PC1_BCKD00(strMa_dviqly, p_thang, p_nam);

                //Goi report de hien thi du lieu tim duoc
                if (dscobo != null)
                {
                    rpt_PC1_BCKD00 crt = new rpt_PC1_BCKD00();
                    crt.SetDataSource(dscobo.Tables["PC1_BCKD00"]);
                    //UtilReport.RptManager.showReportToWin(this, crt, strMa_dviqly);
                    UtilReportOMS.RptManager.showReportToWin(this, crt, strMa_dviqly, new string[] { "Formula1", "Formula2", "Formula3" }, new string[] { "Formula1", "Formula2", "Formula3" }, "CMIS2 - Report: Báo cáo NPC");
                    //Chuyen che do con tro ve trang thai ban dau
                }
                else
                { 
                    MessageBox.Show("Chưa có số liệu báo cáo, hãy nhập báo cáo trước");
                    return;
                
                }
                this.Cursor = null;
            }
            catch (Exception EX)
            {
                MessageBox.Show(EX.ToString());
            }
        }

        private void cmdnhap_Click(object sender, RoutedEventArgs e)
        {
            cmdghi.IsEnabled = true;
            cmdhuy.IsEnabled = true;
            cmddel.IsEnabled = true;
            //cmdin.IsEnabled = false;
            cmdchon.IsEnabled = false;
            cmdnhap.IsEnabled = false;
            dgrNPC_BCKD00.IsReadOnly = false;
            dgrNPC_BCKD00.Focus();
            dgrNPC_BCKD00.SelectedIndex = 0;
        }

        private void cmdhuy_Click(object sender, RoutedEventArgs e)
        {
            cmdchon.IsEnabled = true;
            cmdghi.IsEnabled = false;
            cmddel.IsEnabled = false;
            cmdin.IsEnabled = true;
            cmdnhap.IsEnabled = true;
            cmdhuy.IsEnabled = false;
            dgrNPC_BCKD00.IsReadOnly = true;
            dtbieu00 = dshuynhap.Tables[0].Copy();
            //dtbieu00.Columns["DVT"];
            if (dtbieu00.Rows.Count > 0)
            {
                dgrNPC_BCKD00.ItemsSource = dtbieu00.DefaultView;
                dgrNPC_BCKD00.SelectedIndex = 0;
                txtPT_DG.Text = dtbieu00.Rows[1]["PHANTICH_DANHGIA"].ToString();
                txtYKIEN_KHAC.Text = dtbieu00.Rows[1]["YKIEN_KHAC"].ToString();
            }
        }
        private void kiemtra()
        {
            kt = false;
            for (int i = 0; i < dtbieu00.Rows.Count; i++)
            {
                strThongBao=dtbieu00.Rows[i]["MA_SO"].ToString().Trim();
                for (int j = i+1; j < dtbieu00.Rows.Count; j++)
                {
                    if (dtbieu00.Rows[j]["MA_SO"].ToString().Trim() == strThongBao)
                    {
                        kt = true;
                        intDongTrenGrid = j;
                        break;
                    }
                }
                
            }
        }
        private void kiemtraxoa(Int16 dgrow)
        {
            dem = 0;
            //dtbieu00.Rows.Remove(dgrow);
            MASOGOC = dtbieu00.Rows[dgrow]["MA_SO"].ToString().Trim();
            MASONGON = MASOGOC.Substring(1, MASOGOC.Length - 1);
            for (int i = 0; i < dtbieu00.Rows.Count; i++)
            {
                if (MASONGON == "0000")
                {
                    if (dtbieu00.Rows[i]["MA_SO"].ToString().Trim().Substring(0, 1) == MASOGOC.Substring(0, 1))
                    {
                        dem = dem + 1;
                    }
                }
                
            }
        }
        private void cmdghi_Click(object sender, RoutedEventArgs e)
        {
          try  
           {
            //string ma_so;
            p_thang = Convert.ToInt16(dtpThangNam.SelectedDate.Value.Month);
            p_nam = Convert.ToInt16(dtpThangNam.SelectedDate.Value.Year);
            //Double KH_GIAO, THANG_TH, LUYKE_TH;
            //Int16 stt;
            //string DANH_MUC, DVT, PHANTICH_DANHGIA,YKIEN_KHAC;
            DataRowView drv = dgrNPC_BCKD00.SelectedItem as DataRowView;
            DataSet dsbieu00 = new DataSet();
               kiemtra();
               if (kt == true)
               {
                   MessageBox.Show("Mã số bị trùng");
                   dgrNPC_BCKD00.Focus();
                   dgrNPC_BCKD00.SelectedIndex = intDongTrenGrid;                  
               }
               else
               {
                   if (dsbieu00_Entity.Tables["PC1_BCKD00"].Rows.Count > 0)
                   {
                       //bus_TienichNPC.cls_TienichNPC_bus bus1 = new bus_TienichNPC.cls_TienichNPC_bus();
                       //if (!bus1.DELETE_00(p_nam, p_thang, strMa_dviqly))
                       //{ MessageBox.Show("Lỗi ghi dữ liệu"); return; }
                       //dsbieu00.Tables.Add(dtbieu00);
                       GlobalServices.Service_NPCUS.DELETE_00(p_nam, p_thang, strMa_dviqly);
                       
                       //dungnt su ngay 08/10/2012
                       //for (int i = 0; i < dtbieu00.Rows.Count; i++)
                       //{
                       //    ma_so = dtbieu00.Rows[i]["MA_SO"].ToString().Trim();
                       //    if (ma_so == "")
                       //    {
                       //        strError = "";
                       //    }
                       //    else
                       //    {
                       //        DANH_MUC = dtbieu00.Rows[i]["DANH_MUC"].ToString();
                       //        DVT = dtbieu00.Rows[i]["DVT"].ToString();
                       //        PHANTICH_DANHGIA = txtPT_DG.Text.ToString();
                       //        YKIEN_KHAC = txtYKIEN_KHAC.Text.ToString();
                       //        stt=Convert.ToInt16(dtbieu00.Rows[i]["STT"].ToString());
                       //        if (dtbieu00.Rows[i]["KH_GIAO"].ToString().Trim() == "")
                       //        {
                       //            KH_GIAO = 0;
                       //        }
                       //        else
                       //        {
                       //            KH_GIAO = Convert.ToDouble(dtbieu00.Rows[i]["KH_GIAO"].ToString());
                       //        }
                       //        if (dtbieu00.Rows[i]["THANG_TH"].ToString().Trim() == "")
                       //        {
                       //            THANG_TH = 0;
                       //        }
                       //        else
                       //        {
                       //            THANG_TH = Convert.ToDouble(dtbieu00.Rows[i]["THANG_TH"].ToString());
                       //        }
                       //        if (dtbieu00.Rows[i]["LUYKE_TH"].ToString().Trim() == "")
                       //        {
                       //            LUYKE_TH = 0;
                       //        }
                       //        else
                       //        {
                       //            LUYKE_TH = Convert.ToDouble(dtbieu00.Rows[i]["LUYKE_TH"].ToString());
                       //        }
                       //        //bus_TienichNPC.cls_TienichNPC_bus cls = new bus_TienichNPC.cls_TienichNPC_bus();
                       //        //cls.BCKD00(p_nam, p_thang, strMa_dviqly, stt, DANH_MUC, ma_so, DVT, KH_GIAO, THANG_TH, LUYKE_TH, PHANTICH_DANHGIA, YKIEN_KHAC);
                       //        if (!GlobalServices.Service_NPCUS.BCKD00(p_nam, p_thang, strMa_dviqly, stt, DANH_MUC, ma_so, DVT, KH_GIAO, THANG_TH, LUYKE_TH, PHANTICH_DANHGIA, YKIEN_KHAC))
                       //        //if (!GlobalServices.Service_NPCUS.BCKD00(p_nam, p_thang, strMa_dviqly, stt, DANH_MUC, ma_so, DVT, KH_GIAO, THANG_TH, LUYKE_TH, PHANTICH_DANHGIA, YKIEN_KHAC))
                       //        { MessageBox.Show("Lỗi ghi dữ liệu"); return; }
                       //    }
                       }
                   //bus_TienichNPC.cls_TienichNPC_bus cls1 = new bus_TienichNPC.cls_TienichNPC_bus();
                   //if (!cls1.Insert_BCKD00(dsbieu00_Entity))

                   //if (!cls1.Insert_BCKD00(dsbieu00_Entity))
                   DataTable dt = dsbieu00_Entity.Tables["PC1_BCKD00"] as DataTable;
                   DataRow[] dr = dt.Select();
                   string PHANTICH_DANHGIA = txtPT_DG.Text.ToString();
                   string YKIEN_KHAC = txtYKIEN_KHAC.Text.ToString();
                   for (int i = 0; i < dt.Rows.Count - 1; i++)
                   {
                       dr[i].SetField("PHANTICH_DANHGIA", PHANTICH_DANHGIA);
                       dr[i].SetField("YKIEN_KHAC", YKIEN_KHAC);
                   }
                   if (!GlobalServices.Service_NPCUS.Insert_BCKD00(dsbieu00_Entity))
                               { 
                                 MessageBox.Show("Lỗi ghi dữ liệu"); return; 
                                }
                           
                   
                    GlobalServices.Service_NPCUS.UPDATE_BIEU00(p_nam, p_thang, strMa_dviqly);
                    MessageBox.Show("Ghi dữ liệu thành công");
                   }
                   Initgrid(strMa_dviqly);
                   dgrNPC_BCKD00.IsReadOnly = true;
                   cmdchon.IsEnabled = true;
                   cmdnhap.IsEnabled = true;
                   cmdhuy.IsEnabled = false;
                   cmddel.IsEnabled = true;
                   cmdghi.IsEnabled = false;
                   cmdin.IsEnabled = true;
               
            
           }
          catch (Exception ex)
          {
              MessageBox.Show("Lỗi : " + ex.ToString(), "Thông báo");
              strTim = "";
              return;
          }
    }

        private void cmdchon_Click(object sender, RoutedEventArgs e)
        {
                try
                {
                    for (int i = 0; i < dtHienThi.Rows.Count; i++)
                    {
                        if (cboten_donvi.Text.ToString() == dtHienThi.Rows[i]["TEN_DVIQLY"].ToString())
                        {
                            strMa_dviqly = dtHienThi.Rows[i]["MA_DVIQLY"].ToString();
                        }
                    }
                    
                    // MessageBox.Show(strMa_dviqly);
                    dsbieu00_Entity = GlobalServices.Service_NPCUS.select_PC1_BCKD00(strMa_dviqly, p_thang, p_nam);
                    if (dsbieu00_Entity != null)
                    {
                        if (dsbieu00_Entity.Tables.Count > 0)
                        {
                            if (dsbieu00_Entity.Tables[0].Rows.Count > 0)
                            {
                                Initgrid(strMa_dviqly);

                            }
                            else
                            { khoitao_bieu00(); }
                        }
                        else { khoitao_bieu00(); Initgrid(strMa_dviqly); }
                    }
                    else { khoitao_bieu00(); Initgrid(strMa_dviqly); }
                    
                    p_thang = Convert.ToInt16(dtpThangNam.SelectedDate.Value.Month);
                    p_nam = Convert.ToInt16(dtpThangNam.SelectedDate.Value.Year);

                    
                    if (p_thang == ifthang && p_nam == ifnam)
                        {
                            cmdnhap.IsEnabled = true;
                            cmdhuy.IsEnabled = false;
                            cmdghi.IsEnabled = false;
                            cmddel.IsEnabled = false;
                        }
                        else
                        {
                            cmdnhap.IsEnabled = false;
                            cmdhuy.IsEnabled = false;
                            cmdghi.IsEnabled = false;
                            cmddel.IsEnabled = false;
                        }
                    
                    dgrNPC_BCKD00.IsReadOnly = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Lỗi : " + ex.ToString(), "Thông báo");
                    strTim = "";
                    return;
                }
        }

        private void dtpThangNam_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                cmdchon.Focus();
            }
        }

        private void cboten_donvi_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                dtpThangNam.Focus();
            }
        }

        private void cmdchon_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                txtPT_DG.Focus();
            }
        }
        private void cmddel_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                DataRowView rowView = dgrNPC_BCKD00.SelectedItem as DataRowView;
                DataRow rowdel = rowView.Row;
                    //ds.Tables["Customer"].Rows.Remove(row); 
                row = Convert.ToInt16(dgrNPC_BCKD00.SelectedIndex);
                if (dtbieu00.Rows[row]["MA_SO"].ToString().Trim() == "")
                {
                    //dgrNPC_BCKD00.Items.RemoveAt(dgrNPC_BCKD00.SelectedIndex);
                    dtbieu00.Rows.Remove(rowdel);
                }
                else
                {
                    kiemtraxoa(row);
                    if (dem > 1)
                    {
                        MessageBox.Show("Mã số " + dtbieu00.Rows[row]["MA_SO"].ToString().Trim() + " Đang tồn tại thư mục con không xóa được","NPC-BCKD",MessageBoxButton.OK,MessageBoxImage.Warning);
                    }
                    else
                    {
                        if (MessageBox.Show("Anh(Chị) Có muốn xóa mã số " + dtbieu00.Rows[row]["MA_SO"].ToString().Trim() + " này ko?", "NPC-BCKD", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.Yes) == MessageBoxResult.Yes)
                        {
                            dtbieu00.Rows.Remove(rowdel);
                        }
                    }
                }
                cmdghi.IsEnabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi : " + ex.ToString(), "Thông báo");
                strTim = "";
                return;
            }
        }

        private void dgrNPC_BCKD00_KeyDown(object sender, KeyEventArgs e)
        {
            
        }

        private void txtPT_DG_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                txtYKIEN_KHAC.Focus();
            }
        }

        private void txtYKIEN_KHAC_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                dgrNPC_BCKD00.Focus();
            }
        }

        private void cmdtonghop_Click(object sender, RoutedEventArgs e)
        {
          //tổng hợp trên công ty
          //MessageBox.Show(strMa_dviqly);
            try
            {
                if (strMa_dviqly.ToString().Length > 5)
                {
                    MessageBox.Show("Chức năng tổng hợp chỉ thực hiện ở cấp Công ty", "NPC");
                }
                else
                {
                    //bus_TienichNPC.cls_TienichNPC_bus cls = new bus_TienichNPC.cls_TienichNPC_bus();
                    //cls.tonghop(p_nam, p_thang, strMa_dviqly);
                    GlobalServices.Service_NPCUS.tonghop(p_nam, p_thang, strMa_dviqly);
                    MessageBox.Show("Tổng hợp xong dữ liệu!", "NPC");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi : " + ex.ToString(), "Thông báo");
                return;
            }
        }
    }
}